<template>
  <div class="fillcontain">
    <div class="table_container">
      <div class="search-box">
        <el-row :gutter="5">
          <el-col :md="6">
            <el-input
              v-model="searchInpt"
              placeholder="输入ID/昵称/手机号"
              clearable
              @keyup.enter.native="searchUser()"
            ></el-input>
          </el-col>
          <el-col :md="4">
            <!-- <span class="search-span">年龄</span>
            <el-select v-model="selectAge">
              <el-option v-for="item in selectAgeList" :key="item.age" :label="item.label" :value="item.value"></el-option>
            </el-select> -->
            <el-select
              v-model="userStatusValue"
              placeholder="请选择"
              @change="getUsers()"
            >
              <el-option
                v-for="item in userStatus"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-col>
          <el-col :md="7">
            <!-- <span class="search-span">生理周期</span>
            <el-select v-model="selectCycle">
              <el-option v-for="item in selectCycleList" :key="item.age" :label="item.label" :value="item.value"></el-option>
            </el-select> -->
          </el-col>
          <el-col :md="5">
            <el-button
              type="primary"
              @click="searchUser()"
              icon="el-icon-search"
              :loading="searchLoading"
              plain
            >查询</el-button>
            <el-button
              type="text"
              @click="clearSearch()"
            >清空</el-button>
          </el-col>
        </el-row>
      </div>

      <el-table
        v-loading="tableloading"
        :data="tableData"
        @sort-change="sortUserListId"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column
          property="id"
          label="id"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="nickName"
          label="用户昵称"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="userName"
          label="手机号"
          width="150"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="绑定设备"
          width="150"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="goHardwareEquipment(scope.row,tableData[scope.$index])"
            >{{scope.row.hardwareEquipment}}</el-button>
          </template>
        </el-table-column>
        <!-- <el-table-column>
          <template slot-scope="scope">
            <el-button @click="removeMac(scope.row,tableData[scope.$index])" type="danger" size="small" v-show="scope.row.hardwareEquipment">解绑</el-button>
          </template>
        </el-table-column> -->
        <el-table-column
          property="detectionTime"
          :formatter="formatterTime"
          label="检测时间"
          width="100"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="count"
          style=""
          label="检测次数"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="ovuolaTimeCount"
          style=""
          label="激增次数"
          align="center"
        >
        </el-table-column>
        <!-- <el-table-column property="prePregnant" label="是否备孕">
          <template slot-scope="scope">
            <el-button @click="prePregnant(scope.row)" type="success" size="small" v-if="scope.row.pregnantInfo">是</el-button>
            <el-button @click="prePregnant(scope.row)" type="danger" size="small" v-else>否</el-button>
          </template>
        </el-table-column> -->
        <el-table-column
          property="city"
          label="曲线"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              @click="goGraph(scope.row)"
              type="text"
              size="small"
            >查看</el-button>
            <!-- <el-button @click="goGraph(scope.row)" type="text" size="small">早晚</el-button> -->
          </template>
        </el-table-column>
        <el-table-column
          property="city"
          label="导出"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              @click="download(scope.row)"
              type="text"
              size="small"
            >下载</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div
        class="Pagination"
        style="text-align: left;margin-top: 10px;"
      >
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="count"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import headTop from "../components/headTop";
import { getUserList } from "@/api/getData";
import { ip1Conf } from "@/config/env";
import { formatDate, formatDateTime } from "@/util/common";
export default {
  data() {
    return {
      tableData: [
        {
          id: 971,
          userName: "13888888888",
          nickName: "测试1",
          hardwareEquipment: "F0:B5:D1:81:47:17",
          bindingTime: 1536227107000,
          count: 59,
          detectionHeadChangeTime: 1536227107000,
          prePregnant: false
        }
      ],
      count: 0,
      limit: 20,
      currentPage: 1,
      searchInpt: "",
      searchLoading: false,
      tableloading: false,
      //删选用户类型
      userStatusValue: "",
      userStatus: [
        {
          value: "",
          label: "全部"
        },
        {
          value: "0",
          label: "正式用户"
        },
        {
          value: "1",
          label: "测试用户"
        }
        // {
        //   value: "9",
        //   label: "无效用户"
        // }
      ],
      //选择年龄
      selectAge: "",
      selectAgeList: [
        {
          label: "全部",
          value: ""
        },
        {
          label: "25岁以下",
          value: "1"
        },
        {
          label: "25岁-30岁",
          value: "2"
        },
        {
          label: "31岁-35岁",
          value: "3"
        },
        {
          label: "35岁-40岁",
          value: "4"
        },
        {
          label: "40岁以上",
          value: "5"
        }
      ],
      //选择生理周期
      selectCycle: "",
      selectCycleList: [
        {
          label: "全部",
          value: ""
        },
        {
          label: "24天-35天",
          value: "1"
        },
        {
          label: "36天-45天",
          value: "2"
        },
        {
          label: "24天以上",
          value: "3"
        },
        {
          label: "46天以下",
          value: "4"
        }
      ]
    };
  },
  components: {
    headTop
  },
  created() {
    this.getUsers();
  },
  methods: {
    clearSearch() {
      this.searchInpt = "";
      this.selectAge = "";
      this.selectCycle = "";
    },
    sortUserListId(column, prop, order) {
      console.log(column);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      if (this.searchInpt !== "") {
        const page = "1";
        this.searchUser(page);
      } else {
        this.getUsers();
      }
    },
    async getUsers() {
      this.tableloading = true;
      const Users = await getUserList({
        pageSize: this.limit,
        pageNo: this.currentPage,
        status: this.userStatusValue
      });
      this.tableData = [];
      this.count = Users.data.total;
      this.tableloading = false;
      Users.data.list.forEach(item => {
        const tableData = {};
        tableData.id = item.userId;
        tableData.userName = item.userName;
        tableData.nickName = item.nickName;
        tableData.hardwareEquipment = item.hardwareEquipment;
        tableData.bindingTime = item.bindingTime;
        tableData.detectionTime = item.detectionTime;
        tableData.prePregnant = item.prePregnant;
        tableData.count = item.detectionCount;
        tableData.ovuolaTimeCount = item.ovuolaTimeCount;
        this.tableData.push(tableData);
      });
    },
    download(data) {
      location.href = ip1Conf + "/users/excel?userId=" + data.id;
    },
    goHardwareEquipment(data) {
      this.$router.push({
        path: "equipmentHistorybind",
        query: { hardwareEquipment: data.hardwareEquipment }
      });
    },
    prePregnant(data) {
      this.$confirm("修改, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.pregnant(data);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消修改"
          });
        });
    },
    goGraph(data) {
      this.$router.push({
        path: "pregnancyEcharts",
        query: { userId: data.id }
      });
    },
    formatterTime(row) {
      // return formatDate(row.bindingTime);
      if (row.detectionTime) {
        return formatDateTime(row.detectionTime);
      } else {
        return "";
      }
    },
    async searchUser(page) {
      if (page !== "1") {
        this.currentPage = 1;
      }
      this.searchLoading = true;
      this.tableloading = true;

      const searchUserInfo = await getUserList({
        pageSize: this.limit,
        pageNo: this.currentPage,
        key: this.searchInpt,
        status: this.userStatusValue
      });
      this.searchLoading = false;
      this.tableData = [];
      this.count = searchUserInfo.data.total;
      this.tableloading = false;
      if (searchUserInfo.data.total > 0) {
        searchUserInfo.data.list.forEach(item => {
          const tableData = {};
          tableData.id = item.userId;
          tableData.userName = item.userName;
          tableData.nickName = item.nickName;
          tableData.hardwareEquipment = item.hardwareEquipment;
          tableData.bindingTime = item.bindingTime;
          tableData.detectionTime = item.detectionTime;
          tableData.prePregnant = item.prePregnant;
          tableData.count = item.detectionCount;
          tableData.ovuolaTimeCount = item.ovuolaTimeCount;
          this.tableData.push(tableData);
        });
      } else {
        this.$message({
          type: "info",
          message: "暂无数据"
        });
      }
    }
  }
};
</script>

<style lang="less">
@import "../style/mixin";
.search-box {
  padding: 20px 0;
}
.search-span {
  text-align: right;
  vertical-align: middle;
  float: left;
  font-size: 14px;
  color: #606266;
  line-height: 40px;
  padding: 0 12px;
  box-sizing: border-box;
}
</style>
